<?php

function get_language_data($language_file) {
    $language_data = implode('', file( $language_file));
    preg_match('|Language Name:(.*)$|mi', $language_data, $language_name);
    $name = '';

    if (!empty($language_name[1])) {
        $name = trim($language_name[1]);
    }

    return array('name' => $name);
}

function get_languages($language_folder = '') {
    $languages = array();
    $languages_dir = ROOT . 'www/languages';
    if (is_dir($languages_dir)) {
        if ($dh = opendir($languages_dir)) {
            while (($file = readdir($dh)) !== false) {
                if (strpos($file, '.') !== 0) {
                    if (is_file($languages_dir . '/' . $file)) {
                            $languages_data = get_language_data("$languages_dir/$file");
                            if (!empty($languages_data['name'])) {
                                $languages_data['file_name'] = rtrim($file, '.php');
                                $languages[] = $languages_data;
                            }

                    }
                }
            }
            closedir($dh);
        }
    }
    $english[0] = array('name'=>'English', 'file_name'=>'en');
    $languages = array_merge($english, $languages);
    foreach($languages as $language) {
        $language_options[$language['file_name']] = $language['name'];
    }
    return $language_options;
}
?>
